package com.android.delivery.activity.fragment

import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.R
import com.android.delivery.adapter.LTransOrderAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.entity.Logistics
import com.android.delivery.entity.request.OrderReq
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.adapter_transfer_order.view.*
import kotlinx.android.synthetic.main.include_refresh_layout.*
import org.greenrobot.eventbus.EventBus

/**
 * 揽件中心-转单列表
 */
class LTransOrderListFragment : BaseListFragmet<Logistics>() {

    private val jieDanAdapter: LTransOrderAdapter by lazy {
        LTransOrderAdapter()
    }


    companion object {
        fun newInstance(type: Int): LTransOrderListFragment {
            val fragment = LTransOrderListFragment()
            val bundle = Bundle()
            bundle.putInt(Constants.TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    private var type: Int = 0

    override fun bindRecyclerView(recLv: RecyclerView?) {
        type = arguments!!.getInt(Constants.TYPE)

        recLv!!.adapter = jieDanAdapter
        jieDanAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = jieDanAdapter.getItem(position)
            if (view.id == R.id.tv_1){
                acceptTransfer(item?.logisticsNo!!)
            }else if (view.id == R.id.tv_2){
                rejectTransfer(item?.logisticsNo!!)
            }
        }

    }

    private fun acceptTransfer(logisticsNo: String) {
        ApiService.acceptOrderTransfer(this, logisticsNo, object : DialogCallback<LzyResponse<Any>>() {
            override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                onRefresh(refreshLayout)
                EventBus.getDefault().post(OrderReq())
            }
        })
    }

    private fun rejectTransfer(logisticsNo: String) {
        ApiService.rejectOrderTransfer(this, logisticsNo, object : DialogCallback<LzyResponse<Any>>() {
            override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                onRefresh(refreshLayout)
            }
        })
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.orderTransferList(this, page, object : DialogCallback<LzyResponse<Logistics>>() {
            override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                onGetDataSuc(response!!.body().data.records)
            }
        })
    }

    override fun getData(): MutableList<Logistics> {
        return jieDanAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Logistics>?) {
        if (isRefresh) {
            jieDanAdapter.setNewData(list)
        } else {
            jieDanAdapter.addData(list!!)
        }
    }

}